package cn.com.jd.controller;

import java.io.IOException;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import cn.com.jd.common.JsonDateValueProcessor;
import cn.com.jd.dto.AccountManageDto;
import cn.com.jd.dto.RechargeDto;
import cn.com.jd.dto.StatusDto;
import cn.com.jd.model.GameaccountDto;
import cn.com.jd.model.UserInfoDto;
import cn.com.jd.service.AccountManageService;
import cn.com.jd.service.RechargeService;
import cn.com.jd.service.UserInfoManageService;

@Controller
@SessionAttributes("userId")
public class UserManageController {

	private static Logger logger = Logger.getLogger(RegLogInController.class); 
	@Resource
	private AccountManageService accountManageService;
	@Resource 
	private RechargeService rechargeService;
	@Resource
	private UserInfoManageService userInfoManageService;
	
	@RequestMapping(value = "/getGameAccountList",method= RequestMethod.POST)
	public String getGameAccountList(HttpServletRequest request,HttpServletResponse response,ModelMap model) throws IOException{
		String userid = (String)model.get("userId");
		if(userid != null && userid != ""){
			List<GameaccountDto> gaList = accountManageService.getAccountList(userid);
			Map<String,Object> map = new HashMap<String,Object>();    
		    map.put("accountList", gaList);
		    JsonConfig jsonConfig = new JsonConfig();
		    jsonConfig.registerJsonValueProcessor(Date.class,new JsonDateValueProcessor());
		    String json = JSONObject.fromObject(map, jsonConfig).toString();          
		    //将数据返回  
		    response.setCharacterEncoding("UTF-8");
		    response.flushBuffer();
	        response.getWriter().write(json);  
	        response.getWriter().flush();    
	        response.getWriter().close();
		}
	    return null;
	}
	
	@RequestMapping(value = "/addAccount",method= RequestMethod.POST)
	public String addAccount(HttpServletRequest request,HttpServletResponse response,ModelMap model) throws IOException{
		String userid = (String)model.get("userId");
		if(userid != null && userid != ""){
			AccountManageDto accountManageDto = new AccountManageDto();
			accountManageDto.setUserId(userid);
			accountManageDto.setGameId((String)request.getParameter("gameid"));
			StatusDto statusDto = accountManageService.addAccount(accountManageDto);
			Map<String,Object> map = new HashMap<String,Object>();    
		    map.put("sts", statusDto.getSts());
		    map.put("message", statusDto.getMsg());
		    String json = JSONObject.fromObject(map).toString();
		    //将数据返回  
		    response.setCharacterEncoding("UTF-8");  
		    response.flushBuffer();  
	        response.getWriter().write(json);  
	        response.getWriter().flush();    
	        response.getWriter().close();  
		}
	    return null;
	}
	
	@RequestMapping(value = "/deleteAccount",method= RequestMethod.POST)
	public String deleteAccount(HttpServletRequest request,HttpServletResponse response,ModelMap model) throws IOException{
		String userid = (String)model.get("userId");
		if(userid != null && userid != ""){
			AccountManageDto accountManageDto = new AccountManageDto();
			accountManageDto.setUserId(userid);
			accountManageDto.setGameId((String)request.getParameter("gameid"));
			StatusDto statusDto = accountManageService.deleteAccount(accountManageDto);
			Map<String,Object> map = new HashMap<String,Object>();    
		    map.put("sts", statusDto.getSts());
		    map.put("message", statusDto.getMsg());
		    map.put("maxflag", statusDto.getMaxAccelerateFlag());
		    String json = JSONObject.fromObject(map).toString();          
		    //将数据返回  
		    response.setCharacterEncoding("UTF-8");  
		    response.flushBuffer();  
	        response.getWriter().write(json);
	        response.getWriter().flush();
	        response.getWriter().close();  
		}
	    return null;
	}
	
	@RequestMapping(value = "/accelerateAccount",method= RequestMethod.POST)
	public String accelerateAccount(HttpServletRequest request,HttpServletResponse response,ModelMap model) throws IOException{
		String userid = (String)model.get("userId");
		if(userid != null && userid != ""){
			AccountManageDto accountManageDto = new AccountManageDto();
			accountManageDto.setUserId(userid);
			accountManageDto.setGameId((String)request.getParameter("gameid"));
			StatusDto statusDto = accountManageService.accelerateAccount(accountManageDto);
			Map<String,Object> map = new HashMap<String,Object>();    
		    map.put("sts", statusDto.getSts());
		    map.put("message", statusDto.getMsg());
		    map.put("maxflag", statusDto.getMaxAccelerateFlag());
		    String json = JSONObject.fromObject(map).toString();          
		    //将数据返回  
		    response.setCharacterEncoding("UTF-8");  
		    response.flushBuffer();  
	        response.getWriter().write(json);
	        response.getWriter().flush();
	        response.getWriter().close();  
		}
	    return null;
	}
	
	@RequestMapping(value = "/deaccelerateAccount",method= RequestMethod.POST)
	public String deaccelerateAccount(HttpServletRequest request,HttpServletResponse response,ModelMap model) throws IOException{
		String userid = (String)model.get("userId");
		if(userid != null && userid != ""){
			AccountManageDto accountManageDto = new AccountManageDto();
			accountManageDto.setUserId(userid);
			accountManageDto.setGameId((String)request.getParameter("gameid"));
			StatusDto statusDto = accountManageService.deaccelerateAccount(accountManageDto);
			Map<String,Object> map = new HashMap<String,Object>();    
		    map.put("sts", statusDto.getSts());
		    map.put("message", statusDto.getMsg());
		    map.put("maxflag", statusDto.getMaxAccelerateFlag());
		    String json = JSONObject.fromObject(map).toString();          
		    //将数据返回  
		    response.setCharacterEncoding("UTF-8");  
		    response.flushBuffer();
	        response.getWriter().write(json);
	        response.getWriter().flush();
	        response.getWriter().close();
		}
	    return null;
	}
	
	@RequestMapping(value = "/recharge",method= RequestMethod.POST)
	public String recharge(HttpServletRequest request,HttpServletResponse response,ModelMap model) throws IOException{
		String userid = (String)model.get("userId");
		if(userid != null && userid != ""){
			RechargeDto rechargeDto = new RechargeDto();
			rechargeDto.setUserId(userid);
			rechargeDto.setCardNo((String)request.getParameter("cardno"));
			StatusDto statusDto = rechargeService.recharge(rechargeDto);
			Map<String,Object> map = new HashMap<String,Object>();
		    map.put("sts", statusDto.getSts());
		    map.put("message", statusDto.getMsg());
		    map.put("expiredate", statusDto.getExpireDate());
		    JsonConfig jsonConfig = new JsonConfig();
		    jsonConfig.registerJsonValueProcessor(Date.class,new JsonDateValueProcessor());
		    String json = JSONObject.fromObject(map,jsonConfig).toString();          
		    //将数据返回  
		    response.setCharacterEncoding("UTF-8");  
		    response.flushBuffer();  
	        response.getWriter().write(json);
	        response.getWriter().flush();
	        response.getWriter().close();  
		}
	    return null;
	}
	
	@RequestMapping(value = "/updateUserInfo",method= RequestMethod.POST)
	public String updateUserInfo(HttpServletRequest request,HttpServletResponse response,ModelMap model) throws IOException{
		String userid = (String)model.get("userId");
		if(userid != null && userid != ""){
			UserInfoDto userInfoDto = new UserInfoDto();
			userInfoDto.setUserId(userid);
			userInfoDto.setUserInfo((String)request.getParameter("userinfo"));
			StatusDto statusDto = userInfoManageService.updateUserInfo(userInfoDto);
			Map<String,Object> map = new HashMap<String,Object>();    
		    map.put("sts", statusDto.getSts());
		    map.put("message", statusDto.getMsg());
		    String json = JSONObject.fromObject(map).toString();          
		    //将数据返回  
		    response.setCharacterEncoding("UTF-8");  
		    response.flushBuffer();  
	        response.getWriter().write(json);  
	        response.getWriter().flush();    
	        response.getWriter().close(); 
		}
	    return null;
	}
	
	@RequestMapping(value = "/getUserInfo",method= RequestMethod.POST)
	public String getUserInfo(HttpServletRequest request,HttpServletResponse response,ModelMap model) throws IOException{
		String userid = (String)model.get("userId");
		if(userid != null && userid != ""){
			UserInfoDto userInfoDto = userInfoManageService.getUserInfo(userid);
			Map<String,Object> map = new HashMap<String,Object>();    
		    map.put("userinfo", userInfoDto);
		    JsonConfig jsonConfig = new JsonConfig();
		    jsonConfig.registerJsonValueProcessor(Date.class,new JsonDateValueProcessor());
		    String json = JSONObject.fromObject(map,jsonConfig).toString();          
		    //将数据返回
		    response.setCharacterEncoding("UTF-8");  
		    response.flushBuffer();  
	        response.getWriter().write(json);  
	        response.getWriter().flush();    
	        response.getWriter().close();  
		}
	    return null;
	}
	
	@RequestMapping(value = "/logOut",method= RequestMethod.POST)
	public String logOut(HttpServletRequest request,HttpServletResponse response,ModelMap model) throws IOException{
		model.clear();
		Map<String,Object> map = new HashMap<String,Object>();
	    String json = JSONObject.fromObject(map).toString();          
	    //将数据返回
	    response.setCharacterEncoding("UTF-8");  
	    response.flushBuffer();  
        response.getWriter().write(json);  
        response.getWriter().flush();    
        response.getWriter().close();  
		return null;
	}
}
